var http =require('http');

var fs = require('fs');

var hostname='192.168.13.5';
var port ='3000';

var server= http.createServer(function(req,res){
    if (req.url=='favicon.ico') {return};

    var num =Math.ceil(Math.random()*100000%9);

    var ip=getIp(req);
    console.log(ip+'开始读取第'+num+'张图片');

    fs.readFile('./imgs/'+num+'.jpg',function(err,data){
        console.log(ip+'读取第'+num+'张图片完成');

        res.end(data);
    });
});


server.listen(port,hostname);

//获取url请求客户端ip  封装的方法  仅供使用
var getIp = function(req) {
    var ip = req.headers['x-forwarded-for'] ||
        req.ip ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress || '';
    if(ip.split(',').length>0){
        ip = ip.split(',')[0]
    }
    return ip;
}